﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.Users;

namespace University.Workshop.Presentation.Users
{
    /// <summary>
    /// Interaction logic for PageFirstStepModifyUser.xaml
    /// </summary>
    public partial class PageFirstStepModifyUser : Page, IFirstStepForModifyUser
    {
        private PresenterModifyUserView pInternal;
        private PresenterFirstStepForModifyUser pFirstStepForModifyUser;
        Boolean espaciosNombre;
        Boolean espaciosApellidos;
        Boolean comasTelefonos;

        public PageFirstStepModifyUser(PresenterModifyUserView pExternal)
        {
            InitializeComponent();
            espaciosNombre = true;
            espaciosApellidos = true;
            comasTelefonos = true;
            pInternal = pExternal;
            pFirstStepForModifyUser = new PresenterFirstStepForModifyUser(this);
        }

        private void PersonPhonesTxt_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            pFirstStepForModifyUser.ChargePhonesToList();
        }

        private void PersonPhonesTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            pFirstStepForModifyUser.ChargePhonesToList();
        }

        private void PersonPhonesTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                // e.Handled = !(char.IsDigit(ch) || ch.Equals(','));
                e.Handled = !(char.IsDigit(ch));
            }

            if (!e.Handled)
            {
                comasTelefonos = true;
            }
            else
            {
                if (comasTelefonos)
                {
                    foreach (char ch in e.Text)
                    {
                        e.Handled = !(ch.Equals(','));
                    }
                    if (!e.Handled)
                        comasTelefonos = false;
                }
            }
        }

        private void PersonPhonesTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        private void CantCupyPasteOrCut(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Cut)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Paste)
                e.Handled = true;
        }

        private void PersonNamesTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !char.IsLetter(ch);
            }
            if (!e.Handled)
                espaciosNombre = true;
        }

        private void PersonNamesTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                if (!espaciosNombre)
                    e.Handled = true;
                else
                    espaciosNombre = false;
            }
        }

        private void PersonLastNamesTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !char.IsLetter(ch);
            }
            if (!e.Handled)
                espaciosApellidos = true;
        }

        private void PersonLastNamesTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                if (!espaciosApellidos)
                    e.Handled = true;
                else
                    espaciosApellidos = false;
            }
        }



        #region Miembros de IFirstStepForRegisterUser
        public string PersonId
        {
            set
            {
                PersonCiTxt.Text = value;
            }
        }

        public string PersonNames
        {
            get
            {
                return PersonNamesTxt.Text;
            }
            set
            {
                PersonNamesTxt.Text = value;
            }
        }

        public string PersonLastNames
        {
            get
            {
                return PersonLastNamesTxt.Text;
            }
            set
            {
                PersonLastNamesTxt.Text = value;
            }
        }

        public string PersonNewPhones
        {
            get
            {
                return PersonPhonesTxt.Text;
            }
            set
            {
                PersonPhonesTxt.Text = value;
            }
        }

        public List<string> PersonPhones
        {
            set { PersonPhonesList.ItemsSource = value; }
        }
        #endregion

        #region Miembros de IRegisterBasicView
        public Model.User UserToModify
        {
            get;
            set;
        }

        public void Update()
        {
            pFirstStepForModifyUser.UpdateData();
        }


        public string SystemMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                MessageBox.Show(value);
            }
        }

        public string ErrorMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                MessageBox.Show(value);
            }
        }

        public void ChargeDataToBasic()
        {
            pFirstStepForModifyUser.ChargeDataPerson();
        }

        public void GoNextPage()
        {
            pInternal.GoStepTwo();
        }

        public void GoBackPage()
        {

        }
        #endregion

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Modificar Persona";
        }
    }
}
